package java_thinking.unit_14._14_7_Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019-03-31 16:43
 */

public class Test {
    public static void main(String[] args) {
        //加入这一段可以在磁盘中生成 代理类，让我们看到代理类的真面目
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");

        Interface source = new ElseObject();
        Interface test = (Interface) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader()
                , new Class[]{Interface.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("test");
                if ("doSomething".equals(method.getName())) {
                    return method.invoke(source, null);
                }
                return null;
            }
        });
        test.doSomething();
    }
}